import fs from 'fs';
import path from 'path';
import ini = require('ini');        
import { SVNClient } from '@taiyosen/easy-svn';
import { execSync } from 'child_process';

interface ProjCfg {
    Project: {
        Name: string;
        ProjectType: 'H5' | 'unity';
    };
    Svn: {
        ClientSvn: string;
        DesignerSvn: string;
        SvrSvn: string;
    };
    xml2json: {
        useByte: 'NO' | 'YES';
    };
    Build: {
        ProjRoot: string;
    }
}

export class LayaI18N {
    async process(projName: string, cfgRoot: string, workspacePath: string) {
        let cfgPath = path.join(cfgRoot, `${projName}.ini`);
        if(!fs.existsSync(cfgPath)) {
            console.error(`Configuration file not exists: ${cfgPath}`);
            process.exit(1);
        }

        const cfg = ini.parse(fs.readFileSync(cfgPath, 'utf-8')) as ProjCfg;

        let projPath = cfg.Build.ProjRoot;
        if(!fs.existsSync(projPath)) {
            console.error(`Project path not exists: ${projPath}`);
            process.exit(1);
        }

        // 更新工程目录
        const binRoot = `${projPath}/bin`;
        const binSVN = await this.setSVN(binRoot, `${cfg.Svn.ClientSvn}project/bin`);
        const srcRoot = `${projPath}/src`;
        const srcSVN = await this.setSVN(srcRoot, `${cfg.Svn.ClientSvn}project/src`);

        // 更新工作目录
        fs.existsSync(workspacePath) || fs.mkdirSync(workspacePath);

        const workRoot = `${workspacePath}/${cfg.Project.Name}`;
        fs.existsSync(workRoot) || fs.mkdirSync(workRoot);

        const serverscriptRoot = `${workRoot}/serverscript`;
        const serverscriptSVN = await this.setSVN(serverscriptRoot, `${cfg.Svn.DesignerSvn}script`);

        const servercfgRoot = `${workRoot}/servercfg`;
        const servercfgSVN = await this.setSVN(servercfgRoot, `${cfg.Svn.DesignerSvn}bin/Server`);

        const clientcfgRoot = `${workRoot}/clientcfg`;
        const clientcfgSVN = await this.setSVN(clientcfgRoot, `${cfg.Svn.DesignerSvn}bin/Client`);

        // 语言表目录
        const langRoot = `${projPath}/tools/i18n/dictionary/tw`;
        const langSVN = await this.setSVN(langRoot, `${cfg.Svn.ClientSvn}project/tools/i18n/dictionary/tw`);

        // 执行unity-i18n
        // await cmd('unity-i18n', [`-s \"${projPath}\"`, `-o \"${langRoot}\"`, `-d \"laya\" --task-repalcer \"$workspace=${workRoot}\"`, '-S'])
        execSync(`unity-i18n -s \"${projPath}\" -o \"${langRoot}\" -d \"laya\" --task-replacer \"$workspace=${workRoot}\" -S --silent`);

        // 上传
        await langSVN.addUnversioned();
        await langSVN.commit('更新翻译--来自构建机');
    }

    private async setSVN(root: string, responsitory: string) {
        let svn = new SVNClient();
        svn.setConfig({
            cwd: root,
            responsitory
        });
        try {
            if(fs.existsSync(root)) {
                await svn.cleanup(true);
                await svn.revert();
                await svn.update();
            } else {
                await svn.checkout(responsitory);
            }
        } catch(e) {
            console.error(e);
            process.exit(1);
        }
        return svn;
    }
}